package com.jgp.glmeeting.controller;

import com.jgp.glmeeting.model.MeetingParticipantsRelation;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.service.MeetingParticipantsRelationService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.controller.JGPController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.sys.ui.annotation.UIParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/glMeeting/meetingParticipantsRelationApiController")
public class MeetingParticipantsRelationApiController extends JGPController {
    @Autowired
    private MeetingParticipantsRelationService meetingParticipantsRelationService;

    @Autowired
    private ParticipantsService participantsService;


    /**
     *  更具会议的id  返回制定好参加会议人员的信息
     */
    @RequestMapping("/queryJoinMeetUsers/{meetingID}")
    public Result queryJoinMeetUsers(@UIParam("pager") Pager pager, @PathVariable String meetingID) {

        List<MeetingParticipantsRelation> meetingParticipantsRelations = meetingParticipantsRelationService.queryUsersIDs(meetingID);
        List<String> userIDs = new ArrayList<>();
        for (int i = 0; i < meetingParticipantsRelations.size(); i++) {
            userIDs.add(meetingParticipantsRelations.get(i).getParticipantsId());
        }
        List<Participants> joinParticipants = new ArrayList<>();
        for (int i = 0; i < userIDs.size(); i++) {
            Participants p = participantsService.queryById(userIDs.get(i));
            joinParticipants.add(p);
        }
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category("姓名", "name"));
        categories.add(new GridResult.Category("性别", "sex"));
        categories.add(new GridResult.Category("所属单位", "workUnit"));
        categories.add(new GridResult.Category("职务", "job"));
        categories.add(new GridResult.Category("联系方式", "mobilePhone"));
        return ajaxRe("gdata", categories, joinParticipants, pager);

    }

        /**
         * 批量参加人员
         */
        @RequestMapping("/addJoinUsers")
        public Result addJoinUsers(String[] usersID, String meetingID) {

            for (int i = 0; i < usersID.length; i++) {
                MeetingParticipantsRelation meetingParticipantsRelation = new  MeetingParticipantsRelation(usersID[i], meetingID);
                meetingParticipantsRelationService.addMeetingParticipantsRelation(meetingParticipantsRelation);
            }
            return ajaxRe(true);
        }

    /**
     *删除这个人和会议的关系表的一条数据
     */
    @RequestMapping("/removeJoinUser")
    public Result removeJoinUser(String participantsId,String meetingID){
     List<MeetingParticipantsRelation> m=meetingParticipantsRelationService.queryMeetingParticipantsRelation(participantsId,meetingID);

      meetingParticipantsRelationService.removeById(m.get(0).getId());
        return ajaxRe(true);
    }



}
